package com.zpark.controller;

import com.zpark.exception.*;
import com.zpark.utils.HttpStatusAndMessage;
import com.zpark.utils.JsonUtil;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * 全局统一异常处理
 */
@ControllerAdvice
public class BaseExceptionCatchController {

    @ExceptionHandler
    @ResponseBody
    public JsonUtil<Void> globalExceptionCatch(Exception e){

        String msg = "";

        // 异常处理
        if (e instanceof FindAllPersonNullException) {
            // 查询所有用户数据失败
            msg = e.getMessage();
        } else if (e instanceof UsernameNotFindException){
            // 根据用户名查询用户数据失败
            msg = e.getMessage();
        } else if (e instanceof RemoveByIdPersonException){
            msg = e.getMessage();
        } else if (e instanceof UsernameOrPasswordIsNullException){
            msg = e.getMessage();
        } else if (e instanceof UpdateByIdPersonException){
            msg = e.getMessage();
        }

        return new JsonUtil<>(HttpStatusAndMessage.ERROR, msg, null);

    }
}
